<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of homeAction
 * 默认首页
 * @author xlp
 */
class homeAction extends commonAction {

    protected $menuFile = 'weixin/wxmenu'; //缓存菜单名文件

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    /*
     * 后台默认首页 
     */

    function index() {
        if (parent::_checkIsAdmin()) {
            //访问统计数据展示
            $rs = M('access_stat')->where(array('aid' => admin::$adminInfo['aid']))->limit(0, 7)->order('id desc')->findAll(false);
            $data = array('time' => array(), 'new_user' => array(), 'new_msg' => array(), 'new_cancel' => array());
            if ($rs) {
                $count = count($rs) - 1;
                for ($i = $count; $i >= 0; $i-=1) {
                    $data['time'][] = date('n-d', $rs[$i]['infotime']);
                    $data['new_user'][] = $rs[$i]['new_user'];
                    $data['new_msg'][] = $rs[$i]['new_msg'];
                    $data['new_cancel'][] = $rs[$i]['new_cancel'];
                }
            }
            $code = setEnocde(array('yjb' => 1));
            $url = U('weixin/wxapi/index') . '?token=' . $code;
            $code = urldecode($code);
            $this->getStaticInfo();
            $this->assign(array('rs' => $rs, 'data' => $data, 'url' => $url, 'token' => md5($code . VCODE)));
            $this->display();
        } else {
            jumpTo(U('index/index'));
        }
    }


    /*
     * 义借宝会员订单统计
     */
    function getStaticInfo() {
        //会员
        $members = array();
        $members['total'] = M('user')->where("username <> ''")->count();
        $members['normal'] = M('user')->where("username <> '' AND user_level = 0")->count();
        $members['gold'] = M('user')->where("username <> '' AND user_level = 10")->count();
        //订单
        $orders = array();
        $orders['total'] = M('order')->count();
        $orders['apply'] = M('order')->where(array('order_state' =>10))->count();
        $orders['borrow'] = M('order')->where(array('order_state' =>40))->count();
        $orders['over'] = M('order')->where(array('order_state' =>70))->count();

        //今日注册
        $stime = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        $etime = $stime + 86399;
        $reg = M('user')->where("reg_time >=".$stime." AND reg_time <=".$etime)->count();

        $this->assign(array('members' => $members , 'orders' => $orders , 'reg' => $reg));
    }


    /*
     * 微信访问日志 
     */

    function log() {
        loadAppFile('face.fun');
        $p = $this->_getid('p');
        $uid = $this->_getid('uid', 0);
        $status = $this->_getid('status', 0);
        $ftime = $this->_getid('ftime', 10);
        $lastId = $this->_getid('last_id', 0);
        $isAjax = $this->_getid('is_ajax', 0);
        $st = $this->_get('st', 'content');
        $title = $this->_get('q');
        $wxMenuData = F($this->menuFile);
        $where = array('aid' => admin::$adminInfo['aid']);
        $newData = false;
        if ($title && $st == 'user') {
            $user = D('member')->field('uid')->where(array('LIKE' => array('nickname' => $title)))->findAll(false);
//            z($user);
            if ($user) {
                $uid = array();
                foreach ($user as $v) {
                    $uid[] = $v['uid'];
                }
                $title = '';
            }
        } elseif ($title && $st == 'uid') {
            $uid = $title;
            $title = '';
        }
//        z($uid);
        if ($uid) {
            $where['uid'] = $uid;
        }
        if ($status) {
            $where['msg_type'] = $status;
        }
        if ($title) {
            $where['LIKE'] = array('content' => parent::safeSearch($title));
            $where['msg_type'] = 2;
        }
        if ($lastId) {
            $where['id[>]'] = $lastId;
            $rs = M('access_log')->field('id,uid,msg_id,msg_type,content,create_time')->where($where)->limit(20)->order('id desc')->findAll();
        } else {
            $rs = M('access_log')->field('id,uid,msg_id,msg_type,content,create_time')->where($where)->page($p)->order('id desc')->findAll();
        }
//        z(M('access_log')->getAllSql());
        if ($rs) {
            $lastId = 0;
            foreach ($rs as $k => $v) {
                if (!$lastId) {
                    $lastId = $v['id'];
                }
                if ($v['msg_type'] == 1) {
                    $keyName = md5(str_replace('&amp;', '&', $v['content']));
                    $rs[$k]['content'] = isset($wxMenuData[$keyName]) ? $wxMenuData[$keyName] : $v['content'];
                } elseif ($v['msg_type'] == 2) {
                    $rs[$k]['content'] = getFaceContent($v['content']);
                } elseif ($v['msg_type'] == 4) {
                    $rs[$k]['content'] = '<a href="' . getImgUrl($v['content']) . '" rel="pop">' . $v['content'] . '</a>';
                }
            }
            $newData = true;
        }
        $this->assign(array('rs' => $rs, 'p' => $p, 'total' => M('access_log')->getTotal(), 'lastId' => $lastId, 'ftime' => $ftime, 'status' => $status, 'q' => $title, 'st' => $st, 'uid' => $uid));
        if ($isAjax) {
            $this->JsonReturn('ok', array('lastId' => $lastId, 'count' => count($rs), 'item' => $this->getFetch('log_item'), 'newData' => $newData), 1);
        } else {
            $this->display();
        }
    }

    /*
     * 微信访问统计 
     */

    function stat() {
        load('array.fun');
        $id = $this->_getid('id');
        if ($id) {
            $unsubscribe = array(); //取消关注用户
            $uid = array(); //用户id
            $rs = M('access_stat')->where(array('id' => $id))->find();
            if ($rs['infotime'] < mktime(0, 0, 0, date("m"), date("d"), date("Y"))) {
                $statdetail = F('content/home/statdetail_' . $id);
                if ($statdetail) {
                    $this->assign($statdetail);
                    $this->display('statdetail');
                    return;
                }
            }

            $lastdid = M('access_stat')->field('id as lastdid')->where(array('aid' => admin::$adminInfo['aid']))->order('id desc')->find();
            $rs['lastdid'] = $lastdid['lastdid'];
            //消息
            $msglist = M('access_log')->where('`create_time`>=' . $rs['infotime'] . ' and `create_time`<' . ($rs['infotime'] + 86400))->findAll();
            $msg['total'] = M('access_log')->getTotal();
            $msg['msg2'] = $msg['msg1'] = $msg['msg8'] = $msg['msg9'] = 0;

            foreach ($msglist as $mk => $mv) {
                if (($mv['msg_type'] == 2) || ($mv['msg_type'] == 1) || ($mv['msg_type'] == 8) || ($mv['msg_type'] == 9)) {
                    $msg['msg' . $mv['msg_type']] ++;
                    if ($mv['msg_type'] == 9) {
                        $uid[] = $mv['uid'];
                        $unsubscribe[$mv['uid']] = $mv['create_time'];
                    }
                }
            }
            $unsubscribe = $this->getUnsubscribeInfo($uid, $unsubscribe);
            //用户流失
            $rs['today_new_cancel'] = M('user')->where('`reg_time`>=' . $rs['infotime'] . ' and `reg_time`<' . ($rs['infotime'] + 86400) . ' and status=3')->count();
            //用户入住
            $rs['isbind'] = M('user')->where('`reg_time`>=' . $rs['infotime'] . ' and `reg_time`<' . ($rs['infotime'] + 86400) . ' and `village_id`<>0')->count();
            $sourcelist = M('user')->query('select a.*,b.title as vtitle,b.service_id,b.is_open from __TABLE__ as a left join __PRE__village as b on a.village_id=b.vid where a.reg_time >= ' . $rs['infotime'] . ' and a.reg_time <' . ($rs['infotime'] + 86400));
            $rs['source0'] = $rs['source1'] = $rs['source2'] = $rs['source3'] = $rs['source4'] = $rs['source5'] = 0;
            $rs['servicetotal'] = 0;
            foreach ($sourcelist as $sk => $sv) {
                //来源

                $rs['source' . $sv['source_id']]+=1;

                //分时关注
                for ($i = 0; $i < 24; $i++) {
                    if (!isset($rs['hour'][$i])) {
                        $rs['hour'][$i] = 0;
                    }
                    if (($sv['reg_time'] >= $rs['infotime'] + 3600 * $i) and ( $sv['reg_time'] < $rs['infotime'] + 3600 * ($i + 1))) {
                        $rs['hour'][$i]+=1;
                    }
                }
                //小区人数
                if ($sv['village_id'] > 0) {
                    if (isset($rs['vtop'][$sv['village_id']])) {
                        $rs['vtop'][$sv['village_id']]['count']+=1;
                        $rs['vtop'][$sv['village_id']]['is_open'] = $sv['is_open'];
                    } else {
                        $vtemp['name'] = $sv['vtitle'];
                        $vtemp['count'] = 1;
                        $vtemp['is_open'] = $sv['is_open'];
                        $rs['vtop'][$sv['village_id']] = $vtemp;
                    }
                }

                //所属服务社
                $service = $this->getServiceCache();
                $service[0]['stitle'] = "未开通";
                if (!$sv['service_id']) {
                    $sv['service_id'] = 0;
                }
                $rs['servicetotal']+=1;
                if (isset($rs['service'][$sv['service_id']])) {
                    $rs['service'][$sv['service_id']]['count']+=1;
                } else {
                    $vtemp['name'] = $service[$sv['service_id']]['stitle'];
                    $vtemp['count'] = 1;
                    $rs['service'][$sv['service_id']] = $vtemp;
                }
            }

            $rs['vtop'] = sortByList($rs['vtop'], 'count', 'desc');

            $rs['sourcetotal'] = count($sourcelist);
            $arr = array(); //用户信息
            $isbindMsg = array('isbind' => array('unopen' => 0, 'open' => 0), 'unisbind' => ''); //服务区信息
            foreach ($unsubscribe as $unk => $unv) {
                $arr[$unv['subscribe_time']] = array(
                    'source_id' => $unv['source_id'],
                    'nickname' => $unv['nickname'],
                    'title' => $unv['title'],
                    'invite_uid' => $unv['invite_uid'],
                    'unsubscribe_time' => $unv['unsubscribe_time'],
                );
                if ($unv['title']) {
                    $sbind = $unv['service_id'] ? 'open' : 'unopen';
                    $isbindMsg['isbind'][$sbind] = isset($isbindMsg['isbind'][$sbind]) ? ($isbindMsg['isbind'][$sbind] + 1) : 1;
                } else {
                    $isbindMsg['unisbind'] = isset($isbindMsg['unisbind']) ? ($isbindMsg['unisbind'] + 1) : 1;
                }
            }
            krsort($arr);
            $list = array('rs' => $rs, 'msg' => $msg,
                'unsubscribe' => $arr,
                'regType' => array(0 => '主动关注', 1 => '二维码扫描', 2 => '网站二维码', 3 => '地推', 4 => '第三方', 5 => '商家推广'),
                'isbindMsg' => $isbindMsg,
                'inviteinfo' => $this->getInviteInfo($rs['infotime']),
            );
            if ($rs['infotime'] < mktime(0, 0, 0, date("m"), date("d"), date("Y"))) {
                F('content/home/statdetail_' . $id, $list);
            }
            $this->assign($list);
            $this->display('statdetail');
        } else {
            $starttime = $this->_getid('starttime');
            $endtime = $this->_getid('endtime');
            $p = $this->_getid('p', 0);
            $model = 0;
            if ($starttime && $endtime) {
                $rs = M('access_stat')->where('infotime>=' . $starttime . ' and infotime <=' . $endtime . ' and aid = ' . admin::$adminInfo['aid'])->order('id desc')->findAll();
                $time['endtime'] = $rs[0]['infotime'];
                $time['starttime'] = $rs[count($rs) - 1]['infotime'];
                $getTotal = count($rs);
                $model = 1;
            } else {

                $rs = M('access_stat')->where(array('aid' => admin::$adminInfo['aid']))->page($p)->order('id desc')->findAll();
                $time['endtime'] = $rs[0]['infotime'];
                $time['starttime'] = $rs[count($rs) - 1]['infotime'];
                $getTotal = M('access_stat')->getTotal();
            }


            foreach ($rs as $rk => $rv) {
                $rs[$rk]['isbind'] = M('user')->where('`reg_time`>=' . $rv['infotime'] . ' and `reg_time`<' . ($rv['infotime'] + 86400) . ' and `village_id`<>0')->count();
                //z(M('user')->getAllsql());
                $sourcelist = M('user')->where('`reg_time`>=' . $rv['infotime'] . ' and `reg_time`<' . ($rv['infotime'] + 86400))->findAll(false);
                $source0 = $source1 = $source2 = $source3 = 0;
                foreach ($sourcelist as $sk => $sv) {
                    if ($sv['source_id'] == 0) {
                        $source0++;
                    } elseif ($sv['source_id'] == 1) {
                        $source1++;
                    } elseif ($sv['source_id'] == 2) {
                        $source2++;
                    } else {
                        $source3++;
                    }
                }
                $rs[$rk]['source0'] = $source0;
                $rs[$rk]['source1'] = $source1;
                $rs[$rk]['source2'] = $source2;
                $rs[$rk]['source3'] = $source3;
                $rs[$rk]['today_new_cancel'] = M('user')->where('`reg_time`>=' . $rv['infotime'] . ' and `reg_time`<' . ($rv['infotime'] + 86400) . ' and status=3')->count();
            }

            $data = array('time' => array(), 'new_user' => array(), 'new_msg' => array(), 'new_cancel' => array());
            if ($rs) {
                $count = count($rs) - 1;
                for ($i = $count; $i >= 0; $i-=1) {
                    $data['time'][] = date('n-d', $rs[$i]['infotime']);
                    $data['new_user'][] = $rs[$i]['new_user'];
                    $data['new_msg'][] = $rs[$i]['new_msg'];
                    $data['new_cancel'][] = $rs[$i]['new_cancel'];
                    $data['isbind'][] = $rs[$i]['isbind'];
                    $data['today_new_cancel'][] = $rs[$i]['today_new_cancel'];
                    $data['today_new_user'][] = $rs[$i]['new_user'] - $rs[$i]['today_new_cancel'];
                }
            }
            $this->assign(array('rs' => $rs, 'p' => $p, 'data' => $data, 'total' => $getTotal, 'time' => $time, 'model' => $model));
            $this->display();
        }
    }

    /*
     * 统计数据导出 
     */

    function statdonwload() {
        $starttime = $this->_getid('starttime');
        $endtime = $this->_getid('endtime');
        if ($starttime && $endtime) {
            $rs = M('access_stat')->where('infotime>=' . $starttime . ' and infotime <=' . $endtime . ' and aid = ' . admin::$adminInfo['aid'])->order('id desc')->findAll();
        } else {
            $rs = M('access_stat')->where(array('aid' => admin::$adminInfo['aid']))->order('id desc')->findAll();
        }
        $getTotal = M('access_stat')->getTotal();

        foreach ($rs as $rk => $rv) {
            $rs[$rk]['isbind'] = M('user')->where('`reg_time`>=' . $rv['infotime'] . ' and `reg_time`<' . ($rv['infotime'] + 86400) . ' and `village_id`<>0')->count();
            //z(M('user')->getAllsql());
            $sourcelist = M('user')->where('`reg_time`>=' . $rv['infotime'] . ' and `reg_time`<' . ($rv['infotime'] + 86400))->findAll(false);
            $source0 = $source1 = $source2 = $source3 = 0;
            foreach ($sourcelist as $sk => $sv) {
                if ($sv['source_id'] == 0) {
                    $source0++;
                } elseif ($sv['source_id'] == 1) {
                    $source1++;
                } elseif ($sv['source_id'] == 2) {
                    $source2++;
                } else {
                    $source3++;
                }
            }
            $rs[$rk]['source0'] = $source0;
            $rs[$rk]['source1'] = $source1;
            $rs[$rk]['source2'] = $source2;
            $rs[$rk]['source3'] = $source3;
            $rs[$rk]['today_new_cancel'] = M('user')->where('`reg_time`>=' . $rv['infotime'] . ' and `reg_time`<' . ($rv['infotime'] + 86400) . ' and status=3')->count();
        }

        $data = array('time' => array(), 'new_user' => array(), 'new_msg' => array(), 'new_cancel' => array());
        if ($rs) {
            $count = count($rs) - 1;
            for ($i = $count; $i >= 0; $i-=1) {
                $data['time'][] = date('n-d', $rs[$i]['infotime']);
                $data['new_user'][] = $rs[$i]['new_user'];
                $data['new_msg'][] = $rs[$i]['new_msg'];
                $data['new_cancel'][] = $rs[$i]['new_cancel'];
                $data['isbind'][] = $rs[$i]['isbind'];
                $data['today_new_cancel'][] = $rs[$i]['today_new_cancel'];
                $data['today_new_user'][] = $rs[$i]['new_user'] - $rs[$i]['today_new_cancel'];
            }
        }
        load('csv');
        $myCsv = new csv();
        $list = array();
        foreach ($rs as $val) {
            if ($val['new_user'] > 0) {
                $isbindlv = ceil($val['isbind'] / $val['new_user'] * 100) . "%";
            } else {
                $isbindlv = "无";
            }
            $val['infotime'] = $val['infotime'] ? outTime($val['infotime'], 2) : '';
            $list[] = array(
                $val['id'],
                $val['new_msg'],
                $val['new_user'],
                $val['isbind'],
                $isbindlv,
                $val['today_new_cancel'],
                $val['new_cancel'],
                ($val['new_user'] - $val['today_new_cancel']),
                ($val['new_user'] - $val['new_cancel']),
                "微信关注" . $val['source0'] . "/二维码扫描" . $val['source1'] . "/网站二维码" . $val['source2'] . "/其他" . $val['source3'],
                $val['infotime'],
            );
        }
        $myCsv->write(array('ID', '消息总数', '新增用户', '入住用户', '入住率', '当日关注用户流失', '当日总体用户流失', '当日关注净增用户', '当日总体净增用户', '来源分析', '统计日期'), $list, "统计数据from" . outTime($starttime, 2) . 'to' . outTime($endtime, 2));
    }

    /*
     * 统计数据导出 
     */

    function statdonwloadtest() {
        $act = $this->_getid("act");

        load('csv');
        $myCsv = new csv();
        $list = array();

        if ($act == 1) {
            $rs = M('access_stat')->where(array('aid' => admin::$adminInfo['aid']))->order('id desc')->findAll();

            foreach ($rs as $rk => $rv) {
                $sourcelist = M('user')->where('`reg_time`>=' . $rv['infotime'] . ' and `reg_time`<' . ($rv['infotime'] + 86400))->findAll(false);

                foreach ($sourcelist as $sk => $sv) {
                    //分时关注
                    for ($i = 0; $i < 24; $i++) {
                        if (!isset($rs[$rk]['hour'][$i])) {
                            $rs[$rk]['hour'][$i] = 0;
                        }
                        if (($sv['reg_time'] >= $rs[$rk]['infotime'] + 3600 * $i) and ( $sv['reg_time'] < $rs[$rk]['infotime'] + 3600 * ($i + 1))) {
                            $rs[$rk]['hour'][$i]+=1;
                        }
                    }
                }
                $rs[$rk]['hour'][24] = $rv['infotime'] ? outTime($rv['infotime'], 2) : '';
                $list[] = $rs[$rk]['hour'];
            }
            $myCsv->write(array('0~1时', '1~2时', '2~3时', '3~4时', '4~5时', '5~6时', '6~7时', '7~8时', '8~9时', '9~10时', '10~11时', '11~12时', '12~13时', '13~14时', '14~15时', '15~16时', '16~17时', '17~18时', '18~19时', '19~20时', '20~21时', '21~22时', '22~23时', '23~24时', '日期'), $list, "关注_" . date('y-m-d'));
            //z($rs);
        } elseif ($act == 2) {
            $rs = array();
            $sourcelist = array();
            $sourcelist = M('user')->query('select b.service_id from __TABLE__ as a left join __PRE__village as b on a.village_id=b.vid where a.status=1');
            $rs['servicetotal'] = 0;
            foreach ($sourcelist as $sk => $sv) {

                //所属服务社
                $service = $this->getServiceCache();
                $service[0]['stitle'] = "未开通";
                if (!$sv['service_id']) {
                    $sv['service_id'] = 0;
                }
                $rs['servicetotal']+=1;
                if (isset($rs['service'][$sv['service_id']])) {
                    $rs['service'][$sv['service_id']]['count']+=1;
                } else {
                    $vtemp['name'] = $service[$sv['service_id']]['stitle'];
                    $vtemp['count'] = 1;
                    $rs['service'][$sv['service_id']] = $vtemp;
                }
            }
            $sourcelist = array();
            z($rs);
        } elseif ($act == 3) {
            load('array.fun');
            $rs = array();
            $sourcelist = array();
            $sourcelist = M('user')->query('select a.village_id,b.title as vtitle,b.is_open from __TABLE__ as a left join __PRE__village as b on a.village_id=b.vid where a.status=1');
            foreach ($sourcelist as $sk => $sv) {

                //小区人数
                if ($sv['village_id'] > 0) {
                    if (isset($rs['vtop'][$sv['village_id']])) {
                        $rs['vtop'][$sv['village_id']]['count']+=1;
                        $rs['vtop'][$sv['village_id']]['is_open'] = $sv['is_open'];
                    } else {
                        $vtemp['name'] = $sv['vtitle'];
                        $vtemp['count'] = 1;
                        $vtemp['is_open'] = $sv['is_open'];
                        $rs['vtop'][$sv['village_id']] = $vtemp;
                    }
                }
            }
            $rs['vtop'] = sortByList($rs['vtop'], 'count', 'desc');
            $sourcelist = array();
            $myCsv->write(array('小区名', '人数', '是否开通'), $rs['vtop'], "小区_" . date('y-m-d'));
        }
    }

    /*
     * 编辑微信资料 
     */

    function edit() {
        $this->assign(array('rs' => parent::_getAdminSetting('user_info')));
        $this->display();
    }

    /*
     * 保存资料 
     */

    function save() {
        $objData = array();
        $init = parent::_getDefaultSetting('user_info');
        foreach ($init as $k => $v) {
            $objData[$k] = $this->_post($k, $v);
        }
        parent::_setAdminSetting('user_info', $objData);
        $setting = parent::_getAdminSetting();
        foreach ($setting as $key => $val) {
            $setting[$key] = json_decode($val, true);
        }
        //写入缓存
        if (parent::_checkIsAdmin()) {
            F('weixin/setting', $setting);
        }
        F('weixin/setting_aid_' . admin::$adminInfo['aid'], $setting);
        $this->JsonReturn('操作成功', null, 1);
    }

    function msg() {
        $uid = $this->_getid('uid');
        if (!$uid) {
            $this->returnJson = false;
            $this->JsonReturn('参数丢失');
        }
        $checkTime = TIME - (24 * 60 * 60 * 2);
        //检查用户是否48小时内有回复
        $checkHas = M('access_log')->field('id')->where(array('uid' => $uid, 'create_time[>=]' => $checkTime, 'create_time[<=]' => TIME))->find();
        if (!$checkHas) {
            $this->returnJson = false;
            $this->JsonReturn('用户48小时内没有互动，无法发送消息');
        }
        $this->assign(array('uid' => $uid, 'face' => C('face')));
        $this->display();
    }

    function send() {
        $uid = $this->_post('uid');
        $content = parent::_postContent('content');
        if (!$uid) {
            $this->JsonReturn('参数丢失', array('code' => '', 'msg' => ''), 0);
        }
        if (!$content) {
            $this->JsonReturn('回复内容不能为空', array('code' => '', 'msg' => ''), 0);
        }
        $touser = D('member')->getOpenidByUids($uid);
        if (!$touser) {
            $this->JsonReturn('用户不存在', array('code' => '', 'msg' => ''), 0);
        }
        $sendInfo = array(
            'touser' => $touser,
            'msgtype' => 'text',
            'text' => array(
                'content' => $content
            )
        );
//        z($sendInfo);
        T('weixin/weixin.api');
        $weixinMsgApi = new weixinMsgApi();
        if ($weixinMsgApi->sendCustomMessage($sendInfo)) {
            return $this->JsonReturn('回复成功', null, 1);
        } else {
            return $this->JsonReturn('回复失败', array('code' => $weixinMsgApi->errCode, 'msg' => $weixinMsgApi->errMsg), 0);
        }
    }

    function _empty() {
        return $this->index();
    }

    /**
     * 获取取消关注用户信息
     * @param type $uid  用户id数组
     * @param type $time 用户取消关注时间数组
     * @return type
     */
    private function getUnsubscribeInfo($uid, $time) {
        $data = array();
        $rs = M('user')->query('SELECT a.uid,a.source_id,a.nickname,c.title,a.invite_uid,b.subscribe_time,c.service_id  FROM __TABLE__  AS a'
                . ' LEFT JOIN __PRE__user_detail AS b ON a.uid=b.uid LEFT JOIN __PRE__village AS c ON a.village_id=c.vid  WHERE a.uid IN(' . implode(',', $uid) . ')');
        ksort($rs);
        foreach ($rs as $key => $val) {
            if (isset($rs[$key])) {
                if (isset($rs[$key + 1]) && ($rs[$key]['uid'] == $rs[$key + 1]['uid'])) {
                    unset($rs[$key + 1]);
                }
                $data[$key] = $val;
                $data[$key]['unsubscribe_time'] = $time[$val['uid']];
            }
        }
        return $data;
    }

    /**
     * 获取当日关注人员信息
     * @param type $infotime
     * @return array
     */
    private function getInviteInfo($infotime) {
        $data = array();
        $arr = array();
        $list = array();
        $rs = M('user')->field('invite_uid,uid,village_id')->where(array('reg_time[>=]' => $infotime, 'reg_time[<=]' => ($infotime + 86400)))->findAll(false);
        foreach ($rs as $key => $val) {
            $data[$val['invite_uid']]['uid'] = isset($data[$val['invite_uid']]['uid']) ? ($data[$val['invite_uid']]['uid'] + 1) : 1;
            if ($val['village_id']) {
                $data[$val['invite_uid']]['isbind'] = isset($data[$val['invite_uid']]['isbind']) ? ($data[$val['invite_uid']]['isbind'] + 1) : 1;
            } else {
                $data[$val['invite_uid']]['unisbind'] = isset($data[$val['invite_uid']]['unisbind']) ? ($data[$val['invite_uid']]['unisbind'] + 1) : 1;
            }
        }
        foreach ($data as $key => $val) {
            $list[$key] = $val['uid'];
            $arr[$key]['isbind'] = isset($val['isbind']) ? $val['isbind'] : 0;
            $arr[$key]['unisbind'] = isset($val['unisbind']) ? $val['unisbind'] : 0;
        }
        arsort($list);
        $arr['uid'] = $list;
        // z($arr);
        return $arr;
    }

    function exam_stat() {
        $exam = C('exam/1');
        //z($exam,false);
        $rs = M('exam_stat')->query('SELECT a.*,b.sex FROM __TABLE__ a left join `__PRE__user` b on a.uid =b.uid');
        //z($exam);
        $total = M('exam_stat')->count();
        $this->assign(array('total' => $total));
        $sex = array();
        foreach ($rs as $rk => $rv) {
            if ($rv['sex'] == 1) {
                $sex['1'] ++;
            } elseif ($rv['sex'] == 2) {
                $sex['2'] ++;
            } else {
                $sex['0'] ++;
            }
            $info = json_decode($rv['info']);
            $id = 0;
            foreach ($info as $ik => $iv) {
                if (is_array($iv)) {
                    foreach ($iv as $iik => $iiv) {
                        $exam[$id]['stat'][$iiv] ++;
                    }
                } else {
                    $exam[$id]['stat'][$iv] ++;
                }
                $id++;
            }
        }
        $this->assign(array('total' => $total, 'exam' => $exam, 'sex' => $sex));
        $this->display();
    }


    //基本配置
    public function setting() {
        $_type = $this->_get('_type');
        if(!isHave($_type)) {
            $_type = 'ed';
        }
        $this->assign('_type' , $_type);
        $this->assign('_type_name' , getTypeName($_type));
        $this->assign(array('rs' => parent::_getAdminSetting('basic_setting')));
        $this->display();
    }


    //保存配置
    public function settingsave() {
        $objData = array();
        $init = parent::_getDefaultSetting('basic_setting');

        foreach ($init as $k => $v) {
            $temp = $this->_post($k);
            if(isHave($temp)) { 
                $objData[$k] = $this->_post($k, $v);
            }else if($k == 'soc_min_quota' || $k == 'gold_rate') {
                $_temp = $this->_post($k , 0);
                $objData[$k] = $_temp;
            }    
        }
        // $objData['iou_content'] = $this->_postContent('iou_content');
        parent::_setAdminSetting('basic_setting', $objData);
        $setting = parent::_getAdminSetting();
        foreach ($setting as $key => $val) {
            $setting[$key] = json_decode($val, true);
        }

        //写入缓存
        if (parent::_checkIsAdmin()) {
            F('weixin/setting', $setting);
        }
        $this->JsonReturn('操作成功', null, 1);
    }

}

function getTypeName($type = '') {
    switch ($type) {
        case 'base':
            $objData = '基础信息';
            break;
        case 'ed':
            $objData = '额度';
            break;
        case 'll':
            $objData = '利率';
            break;
        case 'jf':
            $objData = '积分';
            break;
        case 'sxf':
            $objData = '手续费';
            break;        
        default:
            $objData = '额度';
            break;
    }

    return $objData;
}


//根据类型值获取菜单类型
function getMsgType($type = '') {
    switch ($type) {
        //事件推送
        case 1;
        case 'event';
            $objData = '菜单';
            break;
        //用户上行文本信息
        case 2;
        case 'text';
            $objData = '文本';
            break;
        //用户上行位置信息
        case 3;
        case 'location';
            $objData = '位置';
            break;
        //用户上行图片消息
        case 4;
        case 'image';
            $objData = '图片';
            break;
        //用户发送语音信息
        case 5;
        case 'voice';
            $objData = '语音';
            break;
        //用户上行连接信息
        case 6;
        case 'link';
            $objData = '链接';
            break;
        //用户上行视频信息
        case 7;
        case 'video';
            $objData = '视频';
            break;
        //用户关注
        case 8;
        case 'subscribe';
            $objData = '用户关注';
            break;
        //用户上行视频信息
        case 9;
        case 'unsubscribe';
            $objData = '取消关注';
            break;
        //用户上行视频信息
        case 10;
        case 'scan';
            $objData = '扫描二维码';
            break;
        default:
            //用户发送未知信息时回复
            $objData = '未知';
    }
    return $objData;
}
